import 'dart:io';
import 'dart:convert';
import 'package:financing_wallet/home/api/hxh_api.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_network/service_url.dart';
import 'package:get/get.dart';
import 'package:camera/camera.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/log_util.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/report_widget/loading_utils.dart';
import 'package:wallet_utils/toast.dart';
import '../../mine/api/mine_api.dart';
import '../../mine/bean/updateImageBean.dart';
import '../api/hxh_api.dart';
import '../api/home_api.dart';

import '../bean/HXHCheckUser.dart';
import 'state.dart';

class FaceRecognitionLogic extends GetxController {
  final FaceRecognitionState state = FaceRecognitionState();
  late List<CameraDescription> cameras;
  late CameraController controller;
  late Future<void> initializeControllerFuture;
  bool isShowIncoming=false;
  @override
  void onInit() {
    super.onInit();
    /// 初始化摄像头
    isShowIncoming = Get.arguments['isShowIncoming'];
    cameras = Get.arguments['camera'];
    // To display the current output from the Camera,
    // create a CameraController.
    controller = CameraController(
      // Get a specific camera from the list of available cameras.
      cameras[1],
      // Define the resolution to use.
      ResolutionPreset.medium,
    );

    // Next, initialize the controller. This returns a Future.
    initializeControllerFuture = controller.initialize();
  }

  uploadFile(File path) {
    MineApi.uploadFile(
        path,
        BaseCallback(onSuccess: (map) {
          UpdateImageBean updateImageBean = UpdateImageBean.fromJson(map);
          if (updateImageBean.data != null) {
            String? fileUrl = updateImageBean.data?.url;
            compareFaceV2(fileUrl!);
            LogUtils.d('图片路径  ${fileUrl}');
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
          });
        }));
  }

  compareFaceV2(String path) {
    HomeApi.compareFaceV2(
        path,
        BaseCallback(
            onSuccess: (map) {
              if(channel=="ALL_ONLINE"){
                hxhCheckUser();
              }else{
                Get.back();
                if(isShowIncoming){
                  Get.toNamed(BaseRouterConfig.result);
                }else{
                  Get.toNamed(BaseRouterConfig.auditRejection);
                }
              }
            },
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                Get.back();
                controller.resumePreview();
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

    void hxhCheckUser() async{
      HXHApi.checkUser(BaseCallback(onSuccess: (map) {
      HXHCheckUser checkUser = HXHCheckUser.fromJson(map);
      if (checkUser.code == 200) {
        //进件成功
        Get.offNamed(BaseRouterConfig.hxhAudit);
      } else {
        if (isShowIncoming) {
          Get.offNamed(BaseRouterConfig.result);
        } else {
          Get.offNamed(BaseRouterConfig.auditRejection);
        }
      }
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        // Get.back();
        // controller.resumePreview();
        // ToastUtils.showToast(msg ?? "");
        if (isShowIncoming) {
          Get.toNamed(BaseRouterConfig.result);
        } else {
          Get.toNamed(BaseRouterConfig.auditRejection);
        }
      });
    }));
  }

  void submit(File path){
    LoadingUtils.showLoading(Get.context!, isCanBack: false);
    uploadFile(path);
  }

  void backPage() {
    EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.upHomeStatus,value: false));
    Get.until((route) => (route.settings.name == BaseRouterConfig.main));
  }

  @override
  void dispose() {
    // Dispose of the controller when the widget is disposed.
    controller.dispose();
    super.dispose();
  }
}
